<template>
  <!-- 对象数组柱状图 -->
  <div style="height:300px;width:100%;overflow: hidden;">
    <Row
      v-if="option.series.length==0"
      class="bg-fff"
      style="height:300px;"
      type="flex"
      align="middle"
      justify="center"
    >
      <img src="~@/assets/img/efficiencyAnalysis/zwsj.png" height="50%" />
    </Row>
    <div ref="main" style="overflow: hidden;height:300px;width: 100%"></div>
  </div>
</template>

<script>
import { Style } from "./echartsStyle.js";
export default {
  props: {
    viewData: {
      type: Object,
      default: {
        source: [],
        series: []
      }
    },
    lineColor: {
      type: String,
      default: "#666" //测试要求线条颜色以及文字颜色
    },
    legendWH: {
      type: Number,
      default: 10 //测试要求图例circle宽高10px
    }
  },
  data() {
    return {
      option: {
        legend: {
          // ...Style.legend,
          icon: "circle",
          itemWidth: this.legendWH,
          itemHeight: this.legendWH
        },
        tooltip: {
          trigger: "axis",
          axisPointer: {
            // 坐标轴指示器，坐标轴触发有效
            type: "shadow" // 默认为直线，可选为：'line' | 'shadow'
          }
        },
        grid: {
          ...Style.grid
        },
        dataZoom: [
          {
            type: "inside"
          }
        ],
        color: Style.color,
        dataset: {
          source: this.viewData != null ? this.viewData.source : []
        },
        // axisLabel: {
        //   color: "#666" //刻度线标签颜色
        // },
        xAxis: {
          name: this.viewData != null ? this.viewData.xName : "",
          type: "category",
          splitLine: {
            show: false
          },
          nameTextStyle: {
            padding: [27, 0, 0, 0],
            color: this.lineColor //x轴单位的颜色
          },
          axisLine: {
            show: true, //x轴,测试要求加上
            lineStyle: {
              type: "solid",
              color: '#e8e8e8' //x轴线的颜色
            }
          },
          axisLabel: {
            color: this.lineColor //x轴文字的颜色
          },
          axisTick: {
            show: false //测试要求不显示刻度线
          }
        },
        yAxis: {
          name: this.viewData != null ? this.viewData.yName : "",
          type: "value",
          splitLine: {
            show: true,
            lineStyle: {
              type: "dashed",
              color: "#e8e8e8"
            }
          },
          nameTextStyle: {
            padding: [27, 0, 0, 0],
            color: this.lineColor //x轴单位的颜色
          },
          axisLine: {
            show: false, //x轴,测试要求加上
            lineStyle: {
              type: "dashed",
              color: this.lineColor //x轴线的颜色
            }
          },
          axisLabel: {
            color: this.lineColor //x轴文字的颜色
          },
          axisTick: {
            show: false //测试要求不显示刻度线
          }
        },
        series: this.viewData != null ? this.viewData.series : [],
        animationEasing: "elasticOut",
        animationDelayUpdate: function(idx) {
          return idx * 5;
        }
      }
    };
  },
  computed: {},
  watch: {
    viewData: {
      handler(value, oldvalue) {
        setTimeout(() => {
          if ("yName" in value) {
            this.option.yAxis.name = value.yName;
          } else {
            this.viewData.yName = "";
          }
          if ("xName" in value) {
            this.option.xAxis.name = value.xName;
          } else {
            this.viewData.xName = "";
          }
          value.series.map(item => {
            item.barGap = "0";
            item.barMaxWidth = 12;
          });
          this.option.dataset.source = value.source;
          this.option.series = value.series;
          this.$nextTick(() => {
            let myChart = this.$echarts.init(this.$refs.main);
            myChart.setOption(this.option, true);
            myChart.resize();
          });
          // let myChart = this.$echarts.init(this.$refs.main);
          // myChart.setOption(this.option, true);
        }, 1);
      },
      immediate: true
    }
  },
  methods: {},
  created() {},
  mounted() {
    this.$nextTick(() => {
      let myChart = this.$echarts.init(this.$refs.main);
      myChart.setOption(this.option, true);
      myChart.resize();
    });
  },
  beforeCreate() {}, // 生命周期 - 创建之前
  beforeMount() {}, // 生命周期 - 挂载之前
  beforeUpdate() {}, // 生命周期 - 更新之前
  updated() {}, // 生命周期 - 更新之后
  beforeDestroy() {}, // 生命周期 - 销毁之前
  destroyed() {}, // 生命周期 - 销毁完成
  activated() {} // 如果页面有keep-alive缓存功能，这个函数会触发
};
</script>
<style lang='scss' scoped>
</style>
